package 图.无向图;

import 抽象数据类型.Graph;

import java.util.HashMap;
import java.util.Map;

/**
 * @description:
 * @author: ywk
 * @date: 2021-07-28
 */
public class 符号图 {
    public static void main(String[] args) {
        String[][] actors = new String[][]{{"<<MEN WITH GUN>>", "david", "frank"}, {
                "<<SUN RISE IN THE EAST>>", "frank", "cindy"}, {"<<HIS LOVE HIS DREAM>>", "cindy", "david"}
        };
        SymbolGraph symbolGraph = new SymbolGraph(actors);
        Graph g = symbolGraph.G();
        String[] example = new String[]{"<<MEN WITH GUN>>", "cindy", "david"};
        for (String s : example) {
            System.out.println(s + " : ");
            for (Integer index : g.adj(symbolGraph.index(s))) {
                System.out.print("\t" + symbolGraph.name(index));
            }
            System.out.println();
        }
    }

    public static class SymbolGraph {
        private Map<String, Integer> map;
        private String[] keys;
        private Graph graph;

        public SymbolGraph(String[][] actor) {
            map = new HashMap<>();

            for (int i = 0; i < actor.length; i++) {
                for (int j = 0; j < actor[i].length; j++) {
                    if (!map.containsKey(actor[i][j])) {
                        map.put(actor[i][j], map.size());
                    }
                }
            }
            keys = new String[map.size()];
            for (String key : map.keySet()) {
                keys[map.get(key)] = key;
            }
            graph = new Graph(map.size());
            for (String[] act : actor) {
                String mov = act[0];
                for (int i = 1; i < act.length; i++) {
                    graph.addEdge(map.get(mov), map.get(act[i]));
                }
            }
        }

        public boolean contains(String s) {
            return map.containsKey(s);
        }

        public int index(String s) {
            return map.get(s);
        }

        public String name(int v) {
            return keys[v];
        }

        public Graph G() {
            return graph;
        }
    }
}
